package com.example.librarymanagementproject2.service.impl;

import com.example.librarymanagementproject2.Common.exception.GlobalException;
import com.example.librarymanagementproject2.mapper.BookMapper;
import com.example.librarymanagementproject2.model.entity.Book;
import com.example.librarymanagementproject2.model.entity.BookCategory;
import com.example.librarymanagementproject2.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @ClassName BookServiceImpl
 * @Description TODO
 * @Author Kx
 * @Date 2025/10/27 15:29
 * @Version 1.0
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    /**
     * @param category : 图书分类信息
     * @author: kx
     * @date: 2025/10/27 15:57
     * @description: 插入图书分类信息
     */
    @Override
    public void insertBookCategory(BookCategory category) {
        // 判断图书分类是否已存在
        if (bookMapper.getBookCategoryByName(category.getName()) != null) {
            throw new GlobalException(500, "图书分类已存在");
        }
        try {
            // 插入图书分类信息
            bookMapper.insertBookCategory(category);
        } catch (Exception e) {
            throw new GlobalException(500, "插入图书分类信息失败");
        }
    }

    /**
     * @author: kx
     * @date: 2025/10/27 19:12
     * @param book : 图书信息
     * @description: 插入图书信息
    */
    @Override
    public void insertBook(Book book) {
        if (bookMapper.getBookByName(book.getTitle()) != null) {
            throw new GlobalException(500, "图书已存在");
        }
        try {
            // 插入图书信息
            bookMapper.insertBook(book);
        }catch (Exception e) {
            throw new GlobalException(500, "插入图书信息失败");
        }
    }

    /**
     * @author: kx
     * @date: 2025/10/28 16:14
     * @param book : 图书信息
     * @description: 修改图书状态
    */
    @Override
    public void updateBookStatus(Book book) {
        try {
            // 修改图书状态
            bookMapper.updateBookStatus(book);
        } catch (Exception e) {
            throw new GlobalException(500, "修改图书状态失败");
        }
    }
}
